<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_user".
 *
 * @property string $id
 * @property string $realname
 * @property string $phone
 * @property string $info
 * @property integer $fansid
 * @property string $openid
 * @property string $avatar
 * @property string $nickname
 * @property string $address
 * @property string $gender
 * @property integer $status
 */
class BusinessUser extends \yii\db\ActiveRecord
{

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '用户',      //用于修改了什么的显示
        'mainfield' => 'nickname',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];

    const STATUS_BLACK = -1;
    const STATUS_NORMAL = 0;
    public static $statues = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_BLACK => '黑名单',
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'fansid', 'openid', 'avatar', 'nickname', 'gender'], 'required'],
            [['id', 'fansid', 'status','score','createtime'], 'integer'],
            [['realname', 'info', 'avatar', 'address'], 'string', 'max' => 255],
            [['phone'], 'string', 'max' => 16],
            [['openid'], 'string', 'max' => 45],
            [['nickname'], 'string', 'max' => 20],
            [['gender'], 'string', 'max' => 2],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'realname' => '真实姓名',
            'phone' => '手机',
            'info' => '简介',
            'fansid' => '粉丝id',
            'openid' => 'Openid',
            'avatar' => '头像',
            'nickname' => '昵称',
            'address' => '地址',
            'gender' => '性别',
            'status' => '状态',
            'score' => '积分',
            'createtime' => '创建时间',
        ];
    }


    /**
     * @param $score
     * @param $userid
     * @return bool
     * 更新用户积分
     */
    public static function setScore($userid,$score)
    {
        $res = Yii::$app->db->createCommand('update '.self::tableName().' set score=score+:score where id=:id')
            ->bindValues([':score'=>$score,':id'=>$userid])
            ->execute();

        if($res !== false){
            return true;
        }
        return false;
    }

}
